let rewardedVideoAd = null
let rewardedVideoAd1 = null
import Vue from 'vue'
var temp = null


export const mixinAdvertising = {
	mounted() {
		// #ifdef MP-WEIXIN
		// if (!temp) {
		// 	temp = setInterval(() => {
		// 		rewardedVideoAd = null
		// 		rewardedVideoAd1 = null
		// 		var advertising = uni.getStorageSync('advertising')
		// 		if (advertising) {
		// 			this.init()
		// 			clearInterval(temp)
		// 			temp = null
		// 		}

		// 	}, 500)
		// }
		// #endif



	},
	data() {
		return {

		}
	},
	methods: {
		isDownloadShare() {
			var advertising = uni.getStorageSync('advertising')
			var user = uni.getStorageSync('user')
			if (!user) {
				uni.showToast({
					title: "没有登录，请登录",
					icon: "none"
				})
				setTimeout(() => {
					uni.navigateTo({
						url: "/pages/my/login/login"
					})
				}, 500)
				return true
			}
			if (advertising.shareStimulate == 1 && !this.isDownload) {
				// #ifdef MP-WEIXIN
				this.$refs['modal1'].modalName = 'Modal'
				// #endif
				// #ifdef APP-PLUS || H5
				this.toGetVip()
				// #endif
				return true
			}
		},
		downloadShare() {
			var getTime = new Date().getTime()
			setTimeout(() => {
				this.userUpdate(() => {
					setTimeout(() => {
						this.$store.commit('setIsDownload', true)
						this.$refs['modal1'].modalName = ''
						// this.suanTiem1()
					})
				}, 0.5 * 60 * 60 * 1000, 'downloadDate')
			}, 500)
		},
		isOpenGuanGao() {
			var advertising = uni.getStorageSync('advertising')
			if ((advertising.advertisingOpen == 1 || advertising.shareStimulate == 1) && !this.privilege) {
				// #ifdef MP-WEIXIN
				uni.navigateTo({
					url: "/pages/my/award-centre/award-centre"
				})

				// #endif
				// #ifdef APP-PLUS || H5
				this.toGetVip()
				// #endif
				return true
			}
			return false
		},
		// 时间戳计算剩余时间
		getTime2(time) {
			let date = new Date(time);
			let now = new Date();
			let diff = date.getTime() - now.getTime();
			let day = Math.floor(diff / (24 * 3600 * 1000));
			let hour = Math.floor(diff / (3600 * 1000));
			let min = Math.floor(diff / (60 * 1000));
			let sec = Math.floor(diff / 1000);
			if (day > 0) {
				return day + "天";
			} else if (hour > 0) {
				return hour + "小时";
			} else if (min > 0) {
				return min + "分钟";
			} else if (sec > 0) {
				return sec + "秒";
			} else {
				return "已过期";
			}
		},
		toGetVip() {
			let suanTiem1 = this.suanTiem2()
			if (suanTiem1 == '没有登录') {
				uni.showToast({
					title: "没有登录，请登录",
					icon: "none"
				})
				return
			}
			uni.navigateTo({
				url: "/pages/common/get-vip"
			})
		},
		suanTiem2() {
			var userInfo = uni.getStorageSync('user')
			console.log(userInfo, '111111111')
			if (!userInfo) {
				uni.showToast({
					title: "没有登录，请登录",
					icon: "none"
				})
				return "没有登录"
			}

			var date = this.getTime2(Number(userInfo.integral))
			return date
		},
		init1() {
			var advertising = uni.getStorageSync('advertising')
			if (!advertising) {
				return
			}
			var list = advertising['stimulateList'] || []

			var rand = Math.floor(Math.random() * list.length)
			// if (wx.createRewardedVideoAd) {
			// 	rewardedVideoAd = wx.createRewardedVideoAd({
			// 		adUnitId: list[rand] && list[rand].id
			// 	})
			// 	rewardedVideoAd.onLoad(() => {
			// 		console.log('onLoad event emit')
			// 	})
			// 	rewardedVideoAd.onError((err) => {
			// 		console.log('onError event emit', err)
			// 	})
			// 	rewardedVideoAd.onClose((res) => {
			// 		console.log('onClose event emit', res)
			// 	})
			// }
			console.log(list[rand].id,'111111111111')
			if (uni.createRewardedVideoAd) {
				rewardedVideoAd = uni.createRewardedVideoAd({
					adUnitId: list[rand] && list[rand].id
				})
				rewardedVideoAd.onLoad(() => {
					console.log('onLoad event emit')
				})
				rewardedVideoAd.onError((err) => {
					console.log('onError event emit', err)
				})
				rewardedVideoAd.onClose((res) => {
					console.log('onClose event emit', res)
				})
			}
		},
	
		userUpdate(cb, endDate, setType) {
			var userInfo = uni.getStorageSync('user')
			var getTime = new Date().getTime()
			uniCloud.callFunction({
				name: 'user_update',
				data: {
					openid: userInfo.openid,
					endDate: endDate,
					setType: setType
				}
			}).then(res => {
				uni.showToast({
					title: "开启特权成功",
				})
				// this.getUser()
				// uni.setStorageSync('user', res.result.data)
				if (!setType) {
					this.$store.commit('setPrivilege', true)
				}
				if (typeof cb == 'function') {
					cb && cb()
				}
			}).catch((err) => {
				console.log(err)
				uni.showToast({
					title: "开启失败",
					icon: "none"
				})

			});
		},
	
		// 看广告
		kanguangao3(cb) {
			// 用户触发广告后，显示激励视频广告
			
			console.log(rewardedVideoAd,'1111111111111111111')
			if (rewardedVideoAd) {
				rewardedVideoAd.show().catch(() => {
					// 失败重试
					rewardedVideoAd.load()
						.then(() => rewardedVideoAd.show())
						.catch(err => {
							uni.showToast({
								title: "广告显示失败，请点击右上方的三个点，重新进入小程序",
								icon: "none",
								duration: 3000
							})

						})
				})

				// 用户点击了【关闭广告】按钮
				rewardedVideoAd.onClose(res => {
					if (res && res.isEnded) {
						// 正常播放结束，可以下发游戏奖励
						if (typeof cb == 'function') {
							cb && cb()
						}
					} else {
						// 播放中途退出，不下发游戏奖励
						uni.showToast({
							title: "中途退出，不能获取",
							icon: "none"
						})
					}
				})
			}

		},
	}
}


export const advertisingData = {
	computed: {
		advertisingOpen() {
			return this.$store.state.user.advertisingOpen
		},
		privilege() {
			return this.$store.state.user.privilege
		},
		isDownload() {
			return this.$store.state.user.isDownload
		},
		numAdvertising() {
			var advertising = uni.getStorageSync('advertising')
			return advertising.numAdvertising || 0
		}
	},
	created() {

	},
	methods: {}
}
